//$(document).ready(function() {
var admin_HPC = function() {
    function adminHPCLoadJobTable() {
        $('#adminHpcJobInfoTable').dataTable().fnClearTable();
        for (var j = 0; j < allsites.length; j += 1) { (function(siteName) {

                $.post('/' + siteName + '/admin/execute_wait/mpiapi.show_job_for_manage.json', {},
                function(serverResponse) {
                    var user;
                    var tid;
                    var tnc;
                    var inputfile;
                    var command;
                    var state;
                    var jobid;

                    for (var i = 0; i < serverResponse[0]; ++i) {
                        user = serverResponse[1][i].username;
                        tid = serverResponse[1][i].template_id;
                        tnc = serverResponse[1][i].template_node_cnt;
                        inputfile = serverResponse[1][i].inputfile;
                        command = serverResponse[1][i].command;
                        state = serverResponse[1][i].state;
                        jobid = serverResponse[1][i].job_id;
                        //console.log(serverResponse);
                        //console.log(serverResponse[0]);
                        //console.log(serverResponse[1][0].username);
                        $('#adminHpcJobInfoTable').dataTable().fnAddData([
                        siteName, jobid, user, tid, tnc, inputfile, command, state]);
                    }
                },
                'json');
            })(allsites[j]);
        }

    };

    function adminHPCLoadVirtualInfos() {
        $('#adminHpcVirtualClusterInfoTable').dataTable().fnClearTable();
        var currentuser = userid;
        for (var j = 0; j < allsites.length; j += 1) { (function(siteName) {
                $.post('/' + siteName + '/hpc/execute_wait/mpiapi.show_all_cluster.json', {},
                function(serverResponse) {
                    var cid;
                    var iid;
                    var mcpu;
                    var mmem;
                    var scpu;
                    var smem;
                    var scount;
                    var status;
                    var stime;
                    var etime;
                    var ip;
                    var uname;

                    for (var i = 0; i < serverResponse[0]; i += 1) {

                        cid = serverResponse[1][i].cluster_id;

                        iid = serverResponse[1][i].template_id;

                        status = serverResponse[1][i].state;

                        stime = serverResponse[1][i].start_time;

                        etime = serverResponse[1][i].end_time;

                        mcpu = serverResponse[1][i].master_vcpu;

                        mmem = serverResponse[1][i].master_memory;

                        scpu = serverResponse[1][i].slave_vcpu;

                        smem = serverResponse[1][i].slave_memory;

                        scount = serverResponse[1][i].slave_count;

                        ip = serverResponse[1][i].master_ipv6;

                        uname = serverResponse[1][i].username;

                        $('#adminHpcVirtualClusterInfoTable').dataTable().fnAddData([
                        siteName, cid, iid, mcpu, mmem, scpu, smem, scount, status, stime, etime, ip, uname ]);

                        //console.log(serverResponse);
                        //console.log(tid );
                        //  console.log(tnc );
                        //  console.log(inputfile );
                        //  console.log(command );
                        //  console.log(state );
                        //  console.log(result );
                    }
                },
                'json');

            })(allsites[j]);
        }
    };

    adminHPCLoadJobTable();
    adminHPCLoadVirtualInfos();

    $('#adminHpcVirtualClusterInfoRefreshButton').click(function(event) {
        adminHPCLoadVirtualInfos();
    });

    $('#adminHpcJobInfoRefreshButton').click(function(event) {
        adminHPCLoadJobTable();
    });

    //});
};
